package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.util.List;

/**
 * Created by Administrator on 2016/9/25.
 */
public class LookHistoryAdapter extends RecyclerView.Adapter<LookHistoryAdapter.ViewHolder> {

    private Context context;
    private YNStatus mCurStatus;
    private String userid;
    private String avatarPic;


    public LookHistoryAdapter(Context context, YNStatus mCurStatus, String userid, String avatarPic) {
        this.context = context;
        this.mCurStatus = mCurStatus;
        this.userid = userid;
        this.avatarPic = avatarPic;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView sd_look;

        public ViewHolder(View itemView) {
            super(itemView);
            sd_look = ((SimpleDraweeView) itemView.findViewById(R.id.sd_look));
        }

        public void bindData(YNStatus.ViewBean viewBean, int position) {
            if (!checkMe(mCurStatus.getView())) {
                if (position == mCurStatus.getView().size()) {
                    ImageOptHelper.showFrescoTimeLine(Uri.parse(avatarPic), sd_look, context);
                } else {
                    ImageOptHelper.showFrescoTimeLine(Uri.parse(viewBean.getUpic()), sd_look, context);
                }
            } else {
                ImageOptHelper.showFrescoTimeLine(Uri.parse(viewBean.getUpic()), sd_look, context);
            }

        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = LayoutInflater.from(context).inflate(R.layout.look_history_item, parent, false);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {


        if (!checkMe(mCurStatus.getView())) {
            if (position == mCurStatus.getView().size()) {
                holder.bindData(null, position);
            } else {
                YNStatus.ViewBean viewBean = mCurStatus.getView().get(position);
                holder.bindData(viewBean, position);
            }
        } else {
            YNStatus.ViewBean viewBean = mCurStatus.getView().get(position);
            holder.bindData(viewBean, position);
        }
    }

    @Override
    public int getItemCount() {
        if (mCurStatus.getView() == null) {
            return 0;
        } else {
            if (!checkMe(mCurStatus.getView())) {
                return mCurStatus.getView().size() + 1;
            } else {
                return mCurStatus.getView().size();
            }
        }
    }

    public boolean checkMe(List<YNStatus.ViewBean> viewBeanList) {
        for (int i = 0; i < viewBeanList.size(); i++) {
            if (viewBeanList.get(i).getUserid().equals(userid)) {
                return true;
            }
        }
        return false;
    }

}
